package com.ll.domain.dingtalk.base.impl;

import com.alibaba.fastjson.JSONObject;
import com.ll.domain.dingtalk.constant.URLConstant;
import com.ll.base.util.ListUtil;
import com.ll.base.util.ScalarUtil;
import com.ll.domain.dingtalk.base.DingTalkUserBaseService;
import com.ll.domain.dingtalk.dto.DingGroupDTO;
import com.ll.domain.user.convert.UserConvert;
import com.ll.domain.user.dto.UserDTO;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiImChatScenegroupCreateRequest;
import com.dingtalk.api.request.OapiUserGetRequest;
import com.dingtalk.api.request.OapiUserGetuserinfoRequest;
import com.dingtalk.api.request.OapiV2UserListRequest;
import com.dingtalk.api.response.OapiImChatScenegroupCreateResponse;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.dingtalk.api.response.OapiUserGetuserinfoResponse;
import com.dingtalk.api.response.OapiV2UserListResponse;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class DingTalkUserBaseServiceImpl implements DingTalkUserBaseService {
    @Override
    public String login(String token, String requestAuthCode) {
        //获取用户信息
        DingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_GET_USER_INFO);
        OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();
        request.setCode(requestAuthCode);
        request.setHttpMethod("GET");

        OapiUserGetuserinfoResponse response;
        try {
            response = client.execute(request, token);
            log.info(" login -> {} ",JSONObject.toJSONString(response));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return response.getUserid();
    }

    @Override
    public UserDTO getByUserId(String token, String dingTalkUserId) {
        UserDTO sysUser = UserConvert.getSysUser(dingTalkUserId);
        try {
            if(sysUser == null){
                DingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_USER_GET);
                OapiUserGetRequest request = new OapiUserGetRequest();
                request.setUserid(dingTalkUserId);
                request.setHttpMethod("GET");
                OapiUserGetResponse response = client.execute(request, token);
                log.info(" getByUserId -> {} ",JSONObject.toJSONString(response));
                sysUser =  UserConvert.respUserGet2DTO(response);
            }
        } catch (Exception e) {
            log.error(" getByUserIdERROR -> {} ",dingTalkUserId, e);
            return sysUser;
        }
        return sysUser;
    }

    @Override
    public List<UserDTO> getByDeptId(String token, Long dingDeptId) {

        List<UserDTO> result = new ArrayList<>();
        DingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_QUERY_DEPT_USER);
        OapiV2UserListRequest req = new OapiV2UserListRequest();
        OapiV2UserListResponse rsp = null;
        Long nextCursor = ScalarUtil.DEFAULT_LONG_VALUE;
        Long size = 100L;
        do{
            req.setDeptId(dingDeptId);
            req.setCursor(nextCursor);
            req.setSize(size);
            req.setLanguage("zh_CN");
            try {
                rsp = client.execute(req, token);
            } catch (ApiException e) {
                e.printStackTrace();
                return result;
            }
            nextCursor = rsp.getResult().getNextCursor();
            result.addAll(ListUtil.toList(rsp.getResult().getList(), UserConvert::respUserList2DTO));
        }while ( rsp.getResult().getHasMore());

        return result;
    }

    @Override
    public void createGroup(DingGroupDTO dto) {
        try {
            DingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_CREATE_GROUP);
            OapiImChatScenegroupCreateRequest req = new OapiImChatScenegroupCreateRequest();
            req.setOwnerUserId(dto.getMasterDingUid());
            req.setUserIds(dto.getGroupUserIds());
            req.setTitle(dto.getGroupName());
            //固定钉钉群模版
            req.setTemplateId("58312ccf-e40d-4687-89aa-c150d28af908");
            OapiImChatScenegroupCreateResponse rsp = client.execute(req, dto.getToken());
            System.out.println(rsp.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }


}
